package com.demo.java.OD251_300.OD263;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 【速战速决 or 士兵突击】问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/146059586
 */
public class OdMain {
    public static void main(String[] args) {
        java.util.Scanner sc = new java.util.Scanner(System.in);
        int m = sc.nextInt(); // 迷宫的行数
        int n = sc.nextInt(); // 迷宫的列数
        sc.nextLine(); // 读取换行符
        char[][] arr = new char[m][n]; // 迷宫数组
        int startx = -1, starty = -1, endx = -1, endy = -1;

        // 读取迷宫数据并找到起点和终点的位置
        for (int i = 0; i < m; i++) {
            String temp = sc.nextLine();
            arr[i] = temp.toCharArray();
            if (temp.contains("S")) {
                startx = i;
                starty = temp.indexOf("S");
            }
            if (temp.contains("E")) {
                endx = i;
                endy = temp.indexOf("E");
            }
        }

        SolveMethod solu = new SolveMethod();
        solu.help(arr, startx, starty, endx, endy, m, n);
        System.out.println(solu.ans == 999999 ? -1 : solu.ans); // 输出最短路径，若无法到达则输出-1
    }
}